<?php

/**
 * Created by PhpStorm.
 * User: ls721
 * Date: 2016/10/10 0010
 * Time: 17:12
 *
 */
class Domain_Doctor
{

    public function login($username, $password, $ssid,$device)
    {
        $rs = array();
        $model = new Model_Doctor();
        $rs = $model->login($username, $password);
        if (!empty($rs)) {
            $model->update($rs, array('SSID' => $ssid,'Device'=>$device));

        }
        return $rs;

    }

    public function getDoctorInfo($doctor_id)
    {
        $model = new Model_Doctor();

        return $model->get($doctor_id,array(
            "	id	"	,
            "	Registe_Time	"	,
            "	Name	"	,
            "	Status	"	,
            "	Location	"	,
            "	Gender	"	,
            "	Age	"	,
            "	Birth_Date	"	,
            "	Doctor_Card	"	,
            "	Hospital	"	,
            "	DepartName as Depart	"	,
            "	Visible	"	,
            "	PhoneNumber	"	,
            "	IDCard	"	,
            "	SSID	"
        ));
    }

    public function signIn($username, $password,$SSID,$device)
    {
        $model = new Model_Doctor();


        if ($model->isExist($username, 'username') > 0) {
            // var_dump($model1->isExist($username));
            return '';
        } else {
            $Did = uniqid("D");
            $data = array('id' => $Did, 'username' => $username, 'password' => $password,"SSID"=>$SSID,'Device'=>$device);

            $model->insert($data);
            return $model->get($Did,'id as DoctorId');
        }
    }

    public function setDoctorInfo($doctor_id, $name, $gender, $birth_date, $doctor_card, $hospital, $depart, $phone_number,$IDCard)
    {

        $model= new Model_Doctor();
        $model->update($doctor_id,
            array(
                "	Name	"	 =>	$name	,
                "	Gender	"	 =>	$gender	,
                "	Birth_Date	"	 =>	$birth_date	,
                "	Doctor_Card	"	 =>	$doctor_card	,
                "	Hospital	"	 =>	$hospital	,
                "	DepartName	"	 =>	$depart	,
                "	PhoneNumber	"	 =>	$phone_number	,
                "	IDCard	"	 =>	$IDCard
            )
        );
        return $model->get($doctor_id,array( 
                "  Name    ",
                "   Gender  "  ,
                "   Birth_Date  " ,
                "   Doctor_Card "    ,
                "   Hospital    "    ,
                "   DepartName as Depart " ,
                "   PhoneNumber "   ,
                "   IDCard  " )
        );
    }

    public function listDoctors($visible,$status,$depart,$hospital)
    {
        $model = new Model_Doctor();
        $rs = $model->listDoctors($visible,$status,$depart,$hospital);
        for ($i=0;$i<count($rs);$i++) {
            $value=$rs[$i];
        if($value["Status"]=='0'){$value["Status"]="可呼叫";}
        if($value["Status"]=='1'){$value["Status"]="正忙";}
        if($value["Visible"]=='0'){$value["Visible"]="不可定位";}
        if($value["Visible"]=='1'){$value["Visible"]="可定位";}
            $rs[$i]=$value;
    }

        return $rs;

    }

    public function getDoctorLocation($doctor_id)
    {
        $model = new Model_Doctor();
        return $model->get($doctor_id, array('Location', 'Lng', 'Lat'));
    }

    public function setDoctorStatus($doctor_id, $status)
    {
        $model = new Model_Doctor();

        //验证id是否存在
        if ($model->isExist($doctor_id, 'doctor_id') == 0) {
            return '';
        } else {
            $model->update($doctor_id, array('Status' => $status));
            //var_dump($rs);
            return $model->get($doctor_id,'Status');
        }
    }

    public function getDoctorStatus($doctor_id)
    {
        $model = new Model_Doctor();
        return $model->get($doctor_id, 'Status');
    }

    public function setDoctorVisible($doctor_id, $visible)
    {
        $model = new Model_Doctor();

        //验证id是否存在
        if ($model->isExist($doctor_id, 'doctor_id') == 0) {
            return '';
        } else {
            $model->update($doctor_id, array('Visible' => $visible));
            //var_dump($rs);
            return $model->get($doctor_id,'Visible');
        }
    }

    public function getDoctorVisible($doctor_id)
    {
        $model = new Model_Doctor();
        return $model->get($doctor_id, 'Visible');
    }

    public function listDoctorWithDistance($lng, $lat)
    {
        $model = new Model_Doctor();
        $rs = $model->listDoctors("","","","");
        // array_splice($rs,1,1);
        $i = 0;
        for ($i=0;$i<count($rs);$i++) {
            $value= $rs[$i];
                $dis=$this->getDistance($lng, $lat, $value['Lng'], $value['Lat']);
            $value["Distance"]=$dis;
                if($value["Status"]=='0'){$value["Status"]="可呼叫";}
                if($value["Status"]=='1'){$value["Status"]="正忙";}
                if($value["Visible"]=='0'){$value["Visible"]="不可定位";}
                if($value["Visible"]=='1'){$value["Visible"]="可定位";}
                 $rs[$i]=$value;


        }
        //var_dump($model->listDoctors());
        return $rs;
    }

    /**
     * @desc 根据两点间的经纬度计算距离
     * @param float $lat 纬度值
     * @param float $lng 经度值
     */
    function getDistance($lng1, $lat1, $lng2, $lat2)
    {
        $earthRadius = 6367000;

        $lat1 = ($lat1 * pi()) / 180;
        $lng1 = ($lng1 * pi()) / 180;

        $lat2 = ($lat2 * pi()) / 180;
        $lng2 = ($lng2 * pi()) / 180;



        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;

        return round($calculatedDistance);
    }

    public function getEventInfo($event_id)
    {
        $model=new Model_Event();
        $rs=$model->getEventInfo($event_id);
        return $rs;
    }

    public function upload($file){
        //设置上传路径 设置方法参考3.2
        DI()->ucloud->set('default_path', 'comment');
        DI()->ucloud->set('save_path',date('Y/m/d'));

        //新增修改文件名设置上传的文件名称

        DI()->ucloud->set('file_name', '77777kk');

        //上传表单名
        $rs = DI()->ucloud->upfile($file);
        return $rs;

    }

    public function responseCall($response_id)
    {
        $model=new Model_Response();
        $model->update($response_id,array('Status'=>"1"));

        return "success";

    }

    public function catchDispatch($dispatch_id,$status)
    {
        $model=new Model_Dispatch();
        $model->update($dispatch_id,array('Status'=>$status));

        $rs=$model->get($dispatch_id);
        $model1=new Model_Work();
        $model1->insert(array('id'=>uniqid("W"),'Event_id'=>$rs["Event_id"],'Doctor_id'=>$rs["Doctor_id"]));

        $model2=new Model_Event();
        $model2->update($rs['Event_id'],array('Status'=>2));

        return "success";
    }

    public function arriveDispatch($dispatch_id,$status,$lng,$lat)
    {
        $model=new Model_Dispatch();
        $model->update($dispatch_id,array('Status'=>$status));

        $rs=$model->get($dispatch_id);
        $domain1=new Domain_Doctor();
        $domain1->setDoctorLocation($rs["Doctor_id"],$lng,$lat);

        $model2=new Model_Event();
        $model2->update($rs['Event_id'],array('Status'=>3));

        return "success";
    }

    public function setDoctorLocation($doctor_id, $lng,$lat)
    {
        $model = new Model_Doctor();
        $model1 = new Model_Doctor();
        //验证id是否存在
        if ($model1->isExist($doctor_id, 'doctor_id') == 0) {
            return "";
        } else {
            $rs = $model->update($doctor_id, array(
                'Location' => $lng." ".$lat,
                 'Lng' =>$lng ,
                'Lat' => $lat));
            // var_dump($rs);
            return $model->get($doctor_id,array('Location','Lng','Lat' ));
        }

    }

    public function arriveWork($work_id,$status,$lng,$lat)
    {
        $model=new Model_Work();
        $model->update($work_id,array('Status'=>$status));

        $rs=$model->get($work_id);
        $domain1=new Domain_Doctor();
        $domain1->setDoctorLocation($rs["Doctor_id"],$lng,$lat);

        $model2=new Model_Event();
        $model2->update($rs['Event_id'],array('Status'=>4));

        return "success";
    }

    public function setHandle($event_id,$doctor_id,$handle)
    {
        $model=new Model_Handle();
        $Hid=uniqid("H");
        $model->insert(array('id'=>$Hid,'Event_id'=>$event_id,'Doctor_id'=>$doctor_id,'Handle'=>$handle));
        return "success";
    }

    public function getHandle($handle_id)
    {
        $model=new Model_Handle();
        return "success";

    }

    public function listHandle($event_id)
    {
        $model=new Model_Handle();
        return $model->listHandle($event_id);

    }
}